package com.realtyreg.service.validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.realtyreg.domain.entity.users.UserRole;
import com.realtyreg.service.form.NameForm;

/**
 * Created by Petro 04.01.13 18:26
 */
@Component
public class NameValidator implements Validator {

    public boolean supports(Class<?> clazz) {
        return NameForm.class.isAssignableFrom(clazz);
    }

    public void validate(Object target, Errors errors) {
        NameForm nameForm = (NameForm) target;
        String pathPrefix = nameForm.getPathPrefix();
        UserRole role = Enum.valueOf(UserRole.class, nameForm.getRole());
        switch (role) {
            case INDIVIDUAL:
                ValidationUtils.rejectIfEmptyOrWhitespace(errors, pathPrefix + "surName", "validation.nameForm.empty.surName");
                ValidationUtils.rejectIfEmptyOrWhitespace(errors, pathPrefix + "firstName", "validation.nameForm.empty.firstName");
                break;
            case AGENCY:
                ValidationUtils.rejectIfEmptyOrWhitespace(errors, pathPrefix + "name", "validation.nameForm.empty.name");
                break;
        }
    }
}